//
//  CollectionContainer.swift
//  jocool
//
//  Created by tong on 16/6/17.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Eelay

public struct CellRule{
    public var vspace:CGFloat = 0.0 //纵向间隙
    public var hspace:CGFloat = 0.0 //横向间隙
    public var width:CGFloat = 0.0
    public var height:CGFloat = 0.0
    
    public init(_ vspace:CGFloat,_ hspace:CGFloat,_ width:CGFloat,_ height:CGFloat){
        self.vspace = vspace
        self.hspace = hspace
        self.width = width
        self.height = height
    }
}

open class CollectionContainer:ColPaginatorController {
    
    open var sections:TP.section{
        get{
            let sec:TP.section = [
                []
            ]
            return sec
        }
    }
    
    open var selector:TP.selector{
        get{
            let sel = [
                "<\(page_load_section)>":cellType.self,
                ]
            return sel
        }
        
    }

    
    public var content:TermsContainer?{
        didSet{
            switch self.content! {
            case .collection(title: let title, emptyMsg: let emptyMsg, net: let net,let params, node: let node, cellType: let cellType, sectionInset: let sectionInset, cellRule: let cellRule, detailType: let detailType, test: let test):
                self.cellType = cellType
                
                self.detailType = detailType
                
                if let emptyMessage = emptyMsg
                {
                    self.paginator.empty_msg = emptyMessage
                }
                self.title = title
                
                if let emptyMessage = emptyMsg
                {
                    self.paginator.empty_msg = emptyMessage
                }
                
                self.flow.sectionInset = sectionInset
                self.cellSize = [cellRule.width, cellRule.height]
                self.flow.minimumLineSpacing = cellRule.hspace
                self.flow.minimumInteritemSpacing = cellRule.vspace
                
                
                if let p = params
                {
                    paginator.parameters = p
                }
                
                paginator.net = net
                paginator.node = node
                paginator.type = .full
                paginator.in_test = test
                
                //                    self.collectionView.backgroundColor = self.back_color
                
                
            case .list(net: let net, node: let node, emptyMsg:let emptyMsg, params: let params, cellType: let cellType, detailType: let detailType, test: let test):
                if let cellTp = cellType as? UICollectionViewCell.Type
                {
                    self.cellType = cellTp
                }
                self.detailType = detailType
                if let emptyMessage = emptyMsg
                {
                    self.paginator.empty_msg = emptyMessage
                }
                
                if let p = params
                {
                    paginator.parameters = p
                }
                
                paginator.net = net
                paginator.node = node
                paginator.type = .full
                paginator.in_test = test
            default:
                break
            }
            
        }
    }
    
    public var cellSize = CGSize.zero
    
    public var detailType:UIViewController.Type?
    
    public var cellType:UICollectionViewCell.Type = JoCollectionCell.self
    
    public var didSelect:((NSMutableDictionary,CollectionContainer) ->Void)?
    
    public var back_color:UIColor = .table_bg_color


    
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        
       
        
        self.collectionView.backgroundView = UIView().bsui.background(color: self.back_color).owner
    }
    
    open override func reload_page() {
        //首次进入时采用JoLoading 作为加载显示页面
        load(sections: sections, selector: selector)
        loadingV.loading()
        loadMore(page: self.paginator.page_begin)//            refresh()


    }
    
    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    
//    override public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
//        return self.cellSize
//    }
    
    
    open override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return self.cellSize
    }    
    

    open override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let obj = collectionView.cellData(at: indexPath)
        {
            if let detailT = self.detailType
            {
                let ctr = detailT.init()
                if let mctr = ctr as? MoAtrributeProtocol
                {
                    mctr.mo.model = obj
                }
                
                self.navigationController?.pushViewController(ctr, animated: true)
            }
            if let selectA = self.didSelect
            {
                selectA(obj,self)
            }
        }

    }
    
    
}
